package gitee.android.KUI.widget

import android.animation.AnimatorInflater
import android.animation.AnimatorSet
import android.annotation.SuppressLint
import android.app.ActionBar.LayoutParams
import android.content.Context
import android.content.res.Resources.Theme
import android.graphics.*
import android.graphics.Paint.ANTI_ALIAS_FLAG
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.*
import gitee.android.KUI.MeasureText
import gitee.android.KUI.R
import gitee.android.KUI.Utils.clipRound
import gitee.android.KUI.dp
import gitee.android.KUI.px
import kotlin.properties.Delegates


/**
 * @author 程序员Keven
 * 描述 : 土司弹框
 */


val TAG = "Toast";
@SuppressLint("ResourceType")
public class Toast @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,

    ) : View(context, attrs) {


    private var textWidth = 100f;
    private var textHeight = 20f;
    private var Grayity = -1;
    private var text: String = "Toast"

    /**
     * 半径
     */
    private var radius = 0f;

    /**
     * Toast 的 背景颜色
     */
    private var fillColor = Color.WHITE;


    /**
     * 文字颜色
     */

    private var textColor = Color.BLACK;

    private var testSize by Delegates.notNull<Float>();
    private lateinit var textPaint: Paint
    private lateinit var borderPaint: Paint
    var NewsWidth = 0 ;
    init {
        val atter = context!!.obtainStyledAttributes(attrs, R.styleable.Toast);
        Grayity = atter.getInteger(R.styleable.Toast_gravity, 0);
        fillColor = atter.getColor(R.styleable.Toast_toast_background_color, Color.WHITE);
        radius = atter.getDimension(R.styleable.Toast_radius, 0f);
        textColor = atter.getColor(R.styleable.Toast_textColor, Color.BLACK);
        testSize = atter.getDimension(R.styleable.Toast_textSize, 10f)
        if (atter.getString(R.styleable.Toast_text) == null) {
            throw java.lang.IllegalArgumentException("text属性是必须的")
        } else {
            text = atter.getString(R.styleable.Toast_text)!!
        }
        atter.recycle();

        initPaint()
    }

    private fun initPaint() {
        // 文字画笔
        textPaint = Paint();
        textPaint.color = textColor;

        textPaint.textSize = testSize;
        textPaint.isAntiAlias = true


        //边界画笔
        borderPaint = Paint();
        borderPaint.strokeWidth = 0.1f;
        borderPaint.isAntiAlias = true

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        // 获取原始模式
        val oldToastWidth = layoutParams.width;
        val oldToastHeight = layoutParams.height


        if (oldToastWidth == LayoutParams.WRAP_CONTENT || oldToastWidth == LayoutParams.MATCH_PARENT) {
            textWidth = (MeasureText(text, testSize))
            if (Grayity == 0){
                textWidth = (MeasureText(text, testSize))  + (MeasureText(text, testSize))/2
            }

//            textHeight = testSize.toFloat().dp


        }else{
            textWidth = oldToastWidth.toFloat()
        }
        if (oldToastHeight == LayoutParams.MATCH_PARENT || oldToastHeight == LayoutParams.WRAP_CONTENT) {
//            textWidth = (MeasureText(text, testSize.toFloat()) + MeasureText(text, testSize.toFloat()))
            textHeight = testSize.toFloat().dp
        }else{
            textHeight = oldToastHeight.toFloat()
        }

        val newWidthSpec = MeasureSpec.makeMeasureSpec(textWidth.toInt(), MeasureSpec.UNSPECIFIED)
        val newHeightSpec = MeasureSpec.makeMeasureSpec(textHeight.toInt(), MeasureSpec.UNSPECIFIED)
        NewsWidth = MeasureSpec.getSize(newWidthSpec);
        val NewsHeight = MeasureSpec.getSize(newHeightSpec);
        setMeasuredDimension(NewsWidth, NewsHeight);
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        var bitmapCircle =
            Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888)
        var textCavans = Canvas(bitmapCircle)


        var rectF: RectF = RectF();
        rectF.left = 0f;
        rectF.top = 0f;
        rectF.bottom = measuredHeight.toFloat();
        rectF.right = measuredWidth.toFloat();

        var radius = floatArrayOf(
            radius, radius, radius, radius, radius, radius, radius, radius,
        );

        clipRound(canvas, rectF, radius, fillColor)
        if (Grayity == 0) {
            Log.e(TAG, "onDraw: "+( MeasureText(text, testSize) / 4 ) )
            var fl = MeasureText(text, testSize) / 4;
            Log.e(TAG, "onDraw: "+testSize )
            canvas!!.drawText(
                text,
                (MeasureText(text, testSize))/4,
                measuredHeight.toFloat() / 2 + testSize / 2 - 5,
                textPaint
            )

        } else if (Grayity == 1) {

            canvas!!.drawText(
                text,
                (measuredWidth - MeasureText(text, testSize.toFloat())),
                measuredHeight.toFloat() / 2 + testSize / 2 - 5, textPaint
            )
        } else if (Grayity == -1) {
            canvas!!.drawText(text, 0f, measuredHeight.toFloat() / 2 + testSize / 2 - 5, textPaint)
        }


    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)

    }

    override fun dispatchWindowFocusChanged(hasFocus: Boolean) {
        super.dispatchWindowFocusChanged(hasFocus)
        ToastAnim();
    }


    private fun ToastAnim() {
        // 添加动画
        var animationSet = AnimationSet(true);
        var scaleAnimation = ScaleAnimation(
            1.0f, 1.4f, 1.0f, 1.4f,
            Animation.RELATIVE_TO_SELF, 0.3f, Animation.RELATIVE_TO_SELF, 0.3f
        );

        var scaleAnimation1 = ScaleAnimation(
            1.4f, 1.0f, 1.4f, 1.0f,
            Animation.RELATIVE_TO_SELF, 0.3f, Animation.RELATIVE_TO_SELF, 0.3f
        );

        animationSet.addAnimation(scaleAnimation)
        animationSet.addAnimation(scaleAnimation1)
        animationSet.setInterpolator(AccelerateInterpolator());
        animationSet.setDuration(400);
        animationSet.setFillAfter(false);
        startAnimation(animationSet);

        // 延迟消失
        postDelayed(object :  Runnable{
            override fun run() {
                visibility= GONE;
            }
        }, 1500)
    }


}